import types from './types';
import { MutationTree } from 'vuex';

let selectedElementsRate = {// 比例
};
function getTransform (elements) {
  if (!elements || !elements.length) {
    return {};
  }
  let left = 99999;
  let top = 99999;
  let right = 0;
  let bottom = 0;
  for (let element of elements) {
    left = element.transform.left < left ? element.transform.left : left;
    top = element.transform.top < top ? element.transform.top : top;
    right = element.transform.left + element.transform.width > right ? element.transform.left + element.transform.width : right;
    bottom = element.transform.top + element.transform.height > bottom ? element.transform.top + element.transform.height : bottom;
  }
  let width = right - left;
  let height = bottom - top;
  return{
    left,
    top,
    width,
    height
  };
}

const mutations: MutationTree<any> = {
  [types.DEMO] (state, flag): void {
    state.login = flag;
  },
  [types.CHANGE_ELEMENT] (state, newElement): void {
    let id = newElement.id || state.curElementId;
    let curIndex = state.elementList.findIndex(item => item.id === id);
    if (curIndex < 0) {
      return;
    }
    state.elementList.splice(curIndex, 1, { ...state.elementList[curIndex],...newElement });
  },
  [types.REMOVE_ELEMENT] (state, element= {}): void {
    let id = element.id || state.curElementId;
    let curIndex = state.elementList.findIndex(item => item.id === id);
    if (curIndex < 0) {
      return;
    }
    state.curElementId = null;
    state.elementList.splice(curIndex, 1);
  },
  [types.CANCEL_GROUP] (state,elementId): void {
    let curIndex = state.elementList.findIndex(item => item.id === elementId);
    if (curIndex < 0) {
      return;
    }
    state.curElementId = null;
    state.elementList.splice(curIndex, 1);
  },
  [types.ADD_ELEMENT] (state,newElement): void {
    console.log(JSON.stringify([...state.elementList, newElement]))
    state.elementList.push(newElement);
  },
  [types.SELECT_CUR_ELEMENT] (state,id): void {
    state.curElementId = id;
  },
  [types.MENU_VISIBLE] (state, visible): void {
    state.menuVisible = visible;
  },
  [types.ADD_SELECTED_ELEMENTS] (state, elementIds): void {
    let selectedElementIds = [...state.selectedElementIds,...elementIds];
    state.selectedElementIds = [...new Set(selectedElementIds)];
  },
  [types.CANCEL_SELECTED_ELEMENTS] (state): void {
    state.selectedElementIds = [];
    state.selectedElementsTransform = {};
  },
  [types.CHANGE_SELECTED_ELEMENTS_TRANSFORM] (state,{ selectedElements,transform }): void {
    if (selectedElements) {
      let { left,top,width,height } = getTransform(selectedElements);
      state.selectedElementsTransform = {
        left,
        top,
        width,
        height
      };

      selectedElements.forEach(element => {
        selectedElementsRate[element.id] = {
          width: element.transform.width * (1 / width),
          height: element.transform.height * (1 / height),
          left: (element.transform.left - left) * (1 / width),
          top: (element.transform.top - top) * (1 / height)
        };
      });

    }
    if (transform) {
      let old = state.selectedElementsTransform;
      let dx = transform.left - old.left;
      let dy = transform.top - old.top;
      let dw = transform.width - old.width;
      let dh = transform.height - old.height;

      for (let id of state.selectedElementIds) {
        let index = state.elementList.findIndex(item => item.id === id);
        if (index < 0) {
          continue;
        }
        let element = { ...state.elementList[index] };
        element.transform = {
          width: element.transform.width + dw * selectedElementsRate[element.id].width,
          left: element.transform.left + dx + dw * selectedElementsRate[element.id].left,
          height: element.transform.height + dh * selectedElementsRate[element.id].height,
          top: element.transform.top + dy + dh * selectedElementsRate[element.id].top,
          angle: element.transform.angle
        };
        state.elementList.splice(index, 1,element);
      }

      state.selectedElementsTransform = { ...transform };
    }
  },
  [types.FETCH_PROJECT](state, { page, pageSize }): void {

  }

};
export default mutations;
